package com.faf.ui;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.faf.bean.Detail;
import com.faf.bean.Food;
import com.faf.bean.Orders;
import com.faf.dao.DetailDao;
import com.faf.dao.FoodDao;
import com.faf.dao.OrdersDao;
import com.faf.dao.impl.DetailDaoImpl;
import com.faf.dao.impl.FoodDaoImpl;
import com.faf.dao.impl.OrdersDaoImpl;
import com.faf.dao.impl.UserDaoImpl;
import com.faf.service.DetailService;
import com.faf.service.StoreService;
import com.faf.service.UserService;
import com.faf.service.impl.DetailServiceImpl;
import com.faf.service.impl.StoreServiceImpl;
import com.faf.service.impl.UserServiceImpl;
import com.faf.tools.PageUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class OrderManagement
 */
@WebServlet("/OrderManagement")
public class OrderManagement extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OrderManagement() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//璁剧疆璇锋眰鐨勭紪鐮佹牸寮�
		request.setCharacterEncoding("utf-8");
		//
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		/**
		 * 涓�銆佸垵濮嬪寲锛欱BusinessCenter=1
		 * 1銆佺櫥褰�
		 * 2銆佽幏鍙栬鍗曡〃
		 * 3銆侀�氳繃璁㈠崟琛ㄨ幏寰楄彍鍝佸悕绉帮紙鍏堜笉鑰冭檻鍒嗛〉锛�
		 * 4銆佹樉绀鸿鍗曡缁嗕俊鎭�:
		 * 浜屻�佸鐞嗚鍗�
		 * 1銆佹帴鏀惰鍗曪細BusinessCenter=2
		 * 2銆佹嫆缁濊鍗曪細BusinessCenter=3
		 * 
		 */
		String storeNumber=null;
		String password=null;
		int storeId=0;
		
		try
		{
			if(request.getParameter("BusinessCenter")!=null) {
				//System.out.println((String)request.getParameter("BusinessCenter"));
				if(!"1".equals((String)request.getParameter("BusinessCenter"))) {
					storeId = (int)request.getSession().getAttribute("storeId");
					String way = request.getParameter("BusinessCenter");
					int ordersId = Integer.parseInt(request.getParameter("ordersId"));
					OrdersDao ordersDao = new OrdersDaoImpl();
					DetailDao detailDao = new DetailDaoImpl();
					//淇敼璁㈠崟鐨勭姸鎬�
					ordersDao.alterOrdersStateByOrdersId(ordersId,way);
					//鍒涘缓鍒嗛〉
					int currentPageProcessed = Integer.parseInt(request.getParameter("currentPageProcessed"));
					int currentPageUntreated = Integer.parseInt(request.getParameter("currentPageUntreated"));
					int pageTotal = Integer.parseInt(request.getParameter("pageTotal"));
					PageUtils pageProcessed = new PageUtils(currentPageProcessed, pageTotal);
					PageUtils pageUntreated = new PageUtils(currentPageUntreated, pageTotal);
					//鍒嗗埆鏌ヨ鍑哄凡鎺ュ崟鍜屾湭鎺ュ崟鎬绘潯鏁�
					int pageProcessedTotalRecords = ordersDao.getPageProcessedOrdersSum(storeId);
					int pageUntreatedTotalRecords = ordersDao.getPageUntreatedOrdersSum(storeId);
					pageProcessed.setAlterPageNo(pageProcessedTotalRecords);
					pageUntreated.setAlterPageNo(pageUntreatedTotalRecords);
					//鑾峰緱璇ュ簵閾虹殑宸插鐞嗚鍗曪細
					List<Orders> ordersProcessedList = ordersDao.getProcessedOrdersListBystoreId(storeId, pageProcessed.getPageNo(), pageProcessed.getPageSize());
					//鑾峰緱璇ュ簵閾虹殑鏈鐞嗚鍗曪細
					List<Orders> ordersUntreatedList = ordersDao.getUntreatedOrdersListBystoreId(storeId, pageUntreated.getPageNo(), pageUntreated.getPageSize());
					
					pageProcessed.setResult(ordersProcessedList, pageProcessedTotalRecords);
					pageUntreated.setResult(ordersUntreatedList, pageUntreatedTotalRecords);

					List<Detail> detailList = new ArrayList();
					//鑾峰緱搴楅摵鍒嗛〉鍚庣殑鎵�鏈夎鍗�
					List<Orders> ordersList = new ArrayList();
					ordersList.addAll(ordersProcessedList);
					ordersList.addAll(ordersUntreatedList);
					//鑾峰緱璇ュ簵閾虹殑鎵�鏈夎鍗曚俊鎭�
					DetailService detailService = new DetailServiceImpl();
					detailList = detailService.getDetailListByorderNumber(ordersList);
					//鑾峰緱璇ュ簵閾虹殑鎵�鏈夎彍鍝�
					List<Food> foodList = new ArrayList();
					FoodDao foodDao = new FoodDaoImpl();
					foodList = foodDao.getFoodListByStoreId(storeId);
					
					JSONArray orders = JSONArray.fromObject(ordersList);
					JSONArray details = JSONArray.fromObject(detailList);
					JSONArray foods = JSONArray.fromObject(foodList);
					
					JSONObject dataPool = new JSONObject();
					dataPool.put("ordersList", orders);
					dataPool.put("detailList", details);
					dataPool.put("foodList", foods);
					dataPool.put("pageProcessed", pageProcessed);
					dataPool.put("pageUntreated", pageUntreated);
					out.print(dataPool);
				}else {
					OrdersDao ordersDao = new OrdersDaoImpl();
					DetailDao detailDao = new DetailDaoImpl();
					//通过session获取商家id
					storeId = (int) request.getSession().getAttribute("storeId");
					System.out.println("商家id："+storeId);
					
					//设置当前页
					int currentPageProcessed = Integer.parseInt(request.getParameter("currentPageProcessed"));
					int currentPageUntreated = Integer.parseInt(request.getParameter("currentPageUntreated"));
					int pageTotal = Integer.parseInt(request.getParameter("pageTotal"));
					PageUtils pageProcessed = new PageUtils(currentPageProcessed, pageTotal);
					PageUtils pageUntreated = new PageUtils(currentPageUntreated, pageTotal);
					System.out.println("已处理当前页"+currentPageProcessed);
					System.out.println("未处理当前页"+currentPageUntreated);
					System.out.println("总页数页"+pageTotal);
					//获得已处理和未处理的页面记录
					int pageProcessedTotalRecords = ordersDao.getPageProcessedOrdersSum(storeId);
					int pageUntreatedTotalRecords = ordersDao.getPageUntreatedOrdersSum(storeId);
					System.out.println("已处理总页数"+pageProcessedTotalRecords+"未处理总页数"+pageUntreatedTotalRecords);
					//获得已处理当前页的列表
					List<Orders> ordersProcessedList = ordersDao.getProcessedOrdersListBystoreId(storeId, pageProcessed.getPageNo(), pageProcessed.getPageSize());
					//获得未处理当前页列表
					List<Orders> ordersUntreatedList = ordersDao.getUntreatedOrdersListBystoreId(storeId, pageUntreated.getPageNo(), pageUntreated.getPageSize());
					
					pageProcessed.setResult(ordersProcessedList, pageProcessedTotalRecords);
					pageUntreated.setResult(ordersUntreatedList, pageUntreatedTotalRecords);

					List<Detail> detailList = new ArrayList<Detail>();
					//订单列表
					List<Orders> ordersList = new ArrayList<Orders>();
					ordersList.addAll(ordersProcessedList);
					ordersList.addAll(ordersUntreatedList);
					//订单详情列表
					DetailService detailService = new DetailServiceImpl();
					detailList = detailService.getDetailListByorderNumber(ordersList);
					//食品列表
					List<Food> foodList = new ArrayList<Food>();
					FoodDao foodDao = new FoodDaoImpl();
					foodList = foodDao.getFoodListByStoreId(storeId);
					for(int i = 0; i < ordersList.size(); i++) {
						System.out.println("订单ID为："+ordersList.get(i).getOrdersId());
					}
					
					JSONArray orders = JSONArray.fromObject(ordersList);
					JSONArray details = JSONArray.fromObject(detailList);
					JSONArray foods = JSONArray.fromObject(foodList);
					
					JSONObject dataPool = new JSONObject();
					dataPool.put("ordersList", orders);
					dataPool.put("detailList", details);
					dataPool.put("foodList", foods);
					dataPool.put("pageProcessed", pageProcessed);
					dataPool.put("pageUntreated", pageUntreated);
					out.print(dataPool);
				}
				

		}
		} catch (Exception e)
		{
			out.print(e);
			e.printStackTrace();
			
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	private void forward(HttpServletRequest request, HttpServletResponse response, String url, Object... objs) throws ServletException, IOException {
		for (int i = 0; i < objs.length; i+=2)
		{
			request.setAttribute(objs[i]+"", objs[i+1]);
		}
		
		RequestDispatcher rd = request.getRequestDispatcher(url);
		rd.forward(request, response);
	}

}
